package server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarLoader {

    private static final String fileSeparator = System.getProperty("file.separator");

    public void accessJarFile(String jarFileName, String fromDir, String toDir) throws Exception {
        JarFile myJarFile = new JarFile(fromDir + fileSeparator + jarFileName);
        Enumeration myEnum = myJarFile.entries();
        while (myEnum.hasMoreElements()) {
            JarEntry myJarEntry = (JarEntry) myEnum.nextElement();
            if (myJarEntry.getName().equals("jbossall-client.jar")) {
                InputStream is = myJarFile.getInputStream(myJarEntry);
                FileOutputStream fos = new FileOutputStream(toDir + fileSeparator + myJarEntry.getName());
                byte[] b = new byte[1024];
                int len;
                while ((len = is.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fos.close();

                is.close();
                break;
            } else {
                continue;
            }
        }
        myJarFile.close();
    }

}
